home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / applic / ncsa / Mac / Telnet2.6 / prerelease / d5 / Telnet 2.6.1d5.src.sit.hqx / Telnet 2.6.1d5 src / source / main / translate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-26  |  7.4 KB  |  298 lines

  1. /*
  2. *    translate.c
  3. *    written by Roland Mînsson, Lund University Computing Center, Sweden
  4. *    roland_m@ldc.lu.se
  5. *    July 1992
  6. *
  7. *    Modified by Pascal Maes
  8. *    UCL/ELEC
  9. *    Place du Levant, 3
  10. *    B-1348 Louvain-la-Neuve
  11. *****************************************************************
  12. *    Part of:                                                    *
  13. *    NCSA Telnet for the Macintosh                                *
  14. *                                                                *
  15. *    National Center for Supercomputing Applications                *
  16. *    Software Development Group                                    *
  17. *    152 Computing Applications Building                            *
  18. *    605 E. Springfield Ave.                                        *
  19. *    Champaign, IL  61820                                        *
  20. *                                                                *
  21. *    Copyright (c) 1993,                                            *
  22. *    Board of Trustees of the University of Illinois                *
  23. *****************************************************************
  24. *    Modified 7/93 by Jim Browne for NCSA.
  25. */
  26.  
  27. #ifdef MPW
  28. #pragma segment 4
  29. #endif
  30.  
  31. #include <stdio.h>
  32.  
  33. #include "TelnetHeader.h"
  34. #include "general_resrcdefs.h"
  35. #include "debug.h"
  36. #include "wind.h"
  37. #include "vsdata.h"
  38. #include "telneterrors.h"
  39. #include "translate.proto.h"
  40. #include "vsinterf.proto.h"
  41.  
  42. //#define    DEBUG_TRANSLATION
  43. /*************** external variables ***************/
  44.  
  45.  
  46. extern     WindRec *screens;        /* The screen array from maclook.c */
  47. extern    short scrn;                /* The current screen from maclook.c */
  48.  
  49. /*************** global variables ***************/
  50.  
  51. BytePtr    DefaultTable,
  52.         FTPinTable,
  53.         FTPoutTable;
  54.         
  55. Handle    transTablesHdl;
  56. short    nNational;
  57.  
  58. #if 0
  59. Boolean get_trsl (short id, Byte **table)
  60. {
  61.     Handle h;
  62.     long size;
  63.  
  64.     h = GetResource (TRSL,id);
  65.  
  66.     if ((h==NULL) || (ResError()!=noErr)) 
  67.         {
  68.         DoError(106 | RESOURCE_ERRORCLASS, LEVEL2, NULL);
  69.         return (FALSE);
  70.         }
  71.         
  72.     size = GetHandleSize(h);
  73.     if (size != 256) 
  74.         {
  75.         DoError(107 | RESOURCE_ERRORCLASS, LEVEL2, NULL);
  76.         return (FALSE);
  77.         }
  78.  
  79.     HLockHi(h);
  80.     *table = (Byte *) *h;
  81.     return (TRUE);
  82. }
  83. #endif
  84.  
  85. short    transBuffer(short oldtable, short newtable)        /* translate entire buffer */
  86. {
  87.     VSscrn    *vsscreen;
  88.     VSline    *vslin,*p;
  89.     short        lineNo,maxLineNo;
  90.     short        width;                /* allocated witdth of window (80/132) */
  91.     char        tmp[80];                /* only for debugging */
  92.     
  93.     vsscreen = VSwhereis (screens[scrn].vs);
  94.     vslin = vsscreen->buftop;
  95.  
  96. #ifdef DEBUG_TRANSLATION
  97.     putln ("in transBuffer, well and alive");
  98.     sprintf (tmp,"VSgetlines(screens[scrn].vs):%d", VSgetlines(screens[scrn].vs)); putln (tmp);
  99.     sprintf (tmp,"VSmaxwidth(screens[scrn].vs):%d", VSmaxwidth(screens[scrn].vs)); putln (tmp);
  100.     sprintf (tmp,"vsscreen->lines:%d\n",vsscreen->lines); putln (tmp);
  101.     sprintf (tmp,"vsscreen->maxlines:%d\n",vsscreen->maxlines); putln (tmp);
  102.     sprintf (tmp,"vsscreen->numlines:%d\n",vsscreen->numlines); putln (tmp);
  103.     sprintf (tmp,"vsscreen->allwidth:%d\n",vsscreen->allwidth); putln (tmp);
  104.     sprintf (tmp,"vsscreen->maxwidth:%d\n",vsscreen->maxwidth); putln (tmp);
  105.     sprintf (tmp,"vsscreen:%08x\n",vsscreen);  putln (tmp);
  106.     sprintf (tmp,"vslin:%08x\n",vslin);  putln (tmp);
  107.     sprintf (tmp,"next:%08x\n",vslin->next);  putln (tmp);
  108.     sprintf (tmp,"prev:%08x\n",vslin->prev);  putln (tmp);
  109.     sprintf (tmp,"text:%08x\n\n",vslin->text);  putln (tmp);
  110. #endif
  111.  
  112.     width = VSmaxwidth(screens[scrn].vs)+1;    /* VSmaxwidth returns 79 or 131 */
  113.     p = vslin;
  114.     maxLineNo = vsscreen->numlines+VSgetlines(screens[scrn].vs); /* VSgetlines returns 24 or whatever */
  115.     for (lineNo=1; lineNo<=maxLineNo; lineNo++) {
  116.         /*sprintf(tmp,"lineNo:%d, p:%08x, starts with:%c%c%c%c",lineNo,p,*(p->text),*(p->text+1),*(p->text+2),*(p->text+3)); putln(tmp);*/
  117.         if (p==NULL) { putln ("p is NULL"); return (-1); }
  118.         if (p->text==NULL) { putln ("p->text is NULL"); return (-1); }
  119.         
  120.         // First convert the line back to Mac US format, and then to the new format.
  121.         trbuf_nat_mac((unsigned char *)p->text,width, oldtable);
  122.         trbuf_mac_nat((unsigned char *)p->text,width, newtable);
  123.         
  124.         p = p->next;
  125.     }
  126.     sprintf (tmp, "transBuffer:did convert %d lines", lineNo-1); putln (tmp);
  127.     return (0);
  128. }
  129.  
  130. BytePtr        GetTranslationResource(short id)
  131. {
  132.     Handle    h;
  133.     
  134.     h = GetResource(MY_TRSL, id);
  135.     
  136.     if ((h == NULL) || (ResError() != noErr)) {
  137.         // Do nasty mean error here. BUGG
  138.         }
  139.     
  140.     DetachResource(h);
  141.     HLockHi(h);
  142.     return((BytePtr) *h);
  143. }
  144.  
  145. //    table #'s 1...n correspond to tables in our master array, table #0 is the default table
  146. BytePtr        ReturnTablePtr(short table, Boolean out)
  147. {
  148.     if (table > nNational || table < 1) return(DefaultTable + ((out == TRUE) * 256));
  149.     return((BytePtr)(*transTablesHdl + ((table - 1) * 512) + ((out == TRUE) * 256)));
  150. }
  151.  
  152. //    The Default table (i.e. no translation) and the two FTP tables are stored in the 
  153. //     Application's resource fork as resources of type TRSL.  The
  154. //     tables added and removed by the user are stored in the prefs file as resources of
  155. //     type taBL.  This routine loads the default table and the two FTP tables into memory.
  156. //     Failures can only be caused by an incorrect application resource fork.
  157. void    Setup_Default_Tables(void)
  158. {
  159.     DefaultTable = GetTranslationResource(TRSL_DEFAULT_TABLE);
  160.     FTPinTable = GetTranslationResource(TRSL_FTP_TABLE);
  161.     FTPoutTable = FTPinTable + 256;
  162. }
  163.  
  164. /* 
  165. *    Be very careful with calling putln from this procedure, since
  166. *    putln uses the translation tables. If the tables are not setup
  167. *    garbage output will appear. This is not harmful, but very
  168. *    annoying.
  169. */
  170.  
  171. void trInit (MenuHandle    whichMenu)
  172. {
  173.     short    i, nRestaBL;        /* mp: # of resources of type taBL */
  174.     Handle    h;
  175.     short    rsrcID;
  176.     ResType    rsrcType;
  177.     Str255    rsrcName;
  178.     
  179.         
  180.      nNational = 0;
  181.     Setup_Default_Tables();
  182.     transTablesHdl = NewHandle(0);
  183.     
  184.     if ((nRestaBL = CountResources(USER_TRSL)) != nil) {
  185.         for (i = 1; i <= nRestaBL; i++)
  186.         {
  187.             h = GetIndResource(USER_TRSL, i);
  188.             if (ResError() == noErr && (GetHandleSize(h) == 512)) {
  189.                 GetResInfo(h, &rsrcID, &rsrcType, rsrcName);
  190.                 nNational++;
  191.  
  192.                 // Insert the table's name in the Translation menu
  193.                 AppendMenu(whichMenu, "\pDoh");
  194.                 SetItem(whichMenu, nNational+1, rsrcName);    // No metas!
  195.                 
  196.                 // Now append the table's data to the master array of table data
  197.                 HUnlock(transTablesHdl);
  198.                 SetHandleSize(transTablesHdl, (nNational * 512));
  199.                 HLockHi(transTablesHdl);
  200.                 HLock(h);
  201.                 BlockMove(*h, (*transTablesHdl) + ((nNational - 1) * 512), 512);
  202.                 
  203.                 // Release the resource
  204.                 ReleaseResource(h);
  205.                 }
  206.         }
  207.  
  208.     }
  209.     
  210.     CalcMenuSize(whichMenu);
  211. }
  212.  
  213. /*    Converts a char from 8-bit National to 8-bit Macintosh */
  214. void    trbuf_nat_mac(unsigned char *buf, short len, short table)
  215. {
  216.     short            i;
  217.     unsigned char    *p;
  218.     BytePtr            table_data;
  219.  
  220.     table_data = ReturnTablePtr(table, FALSE);
  221.     
  222.     for (i=0,p=buf; i<len; i++,p++)
  223.     {
  224.         *p = table_data[(short)*p];
  225.     }
  226.             
  227. }
  228.  
  229. unsigned char    ftp_iso_mac(unsigned char *ascii)
  230. {
  231.     short    b;
  232.     
  233.     b = (short) *ascii;
  234.     *ascii = FTPinTable[b];
  235.     return (*ascii);
  236. }
  237.  
  238.  
  239. void    trbuf_ftp_mac(unsigned char *buf, short len)
  240. {
  241.     short            i;
  242.     unsigned char    ascii;
  243.     unsigned char    *p;
  244.  
  245.     for (i=0,p=buf; i<len; i++,p++)
  246.     {
  247.         ascii = *p;
  248.         *p = ftp_iso_mac(&ascii);
  249.     }
  250.             
  251. }
  252.  
  253.  
  254. /*    Converts a char from 8-bit Macintosh to 8-bit National */
  255. unsigned char    mac_nat(unsigned char *ascii, short table)
  256. {
  257.     short    b;
  258.     BytePtr    table_data = ReturnTablePtr(table, TRUE);
  259.     
  260.     b = (short) *ascii;
  261.     *ascii = table_data[b];
  262.     return (*ascii);
  263. }
  264.  
  265. unsigned char    ftp_mac_iso(unsigned char *ascii)
  266. {
  267.     short    b;
  268.     
  269.     b = (short) *ascii;
  270.     *ascii = FTPoutTable[b];
  271.     return (*ascii);
  272. }
  273.  
  274. void    trbuf_mac_nat(unsigned char *buf, short len, short table)
  275. {
  276.     short            i;
  277.     unsigned char    ascii;
  278.     unsigned char    *p;
  279.     
  280.     for (i=0,p=buf; i<len; i++,p++)
  281.     {
  282.         ascii = *p;
  283.         *p = mac_nat(&ascii, table);
  284.     }
  285. }
  286.  
  287. void    trbuf_mac_ftp(unsigned char *buf, short len)
  288. {
  289.     short            i;
  290.     unsigned char    ascii;
  291.     unsigned char    *p;
  292.     
  293.     for (i=0,p=buf; i<len; i++,p++)
  294.     {
  295.         ascii = *p;
  296.         *p = ftp_mac_iso(&ascii);
  297.     }
  298. }